package com.deng.etl.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import com.deng.common.SQLHelper;
import com.deng.etl.vo.EtlDailyVo;
import com.deng.util.DateTools;

public class EtlAuto extends SQLHelper{
	//0  完成   1 进行中   2 失败
	
	public void deleteDateByHand(EtlDailyVo vo,Date fromDate,Date toDate){
		try{
			if(vo.status == 1){
				return;
			}
			new EtlThread(vo,fromDate,toDate,3).start();
			return;
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void updateDateByHand(EtlDailyVo vo,Date fromDate,Date toDate){
		try{
			if(vo.status == 1){
				return;
			}
			new EtlThread(vo,fromDate,toDate,2).start();
			return;
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}	
	public void insertDateByHand(EtlDailyVo vo,Date fromDate,Date toDate){
		try{
			if(vo.status == 1){
				return;
			}
			new EtlThread(vo,fromDate,toDate,1).start();
			return;
		}catch(Exception ex){
			ex.printStackTrace();
		}
						
	}
	public List<EtlDailyVo> getEtlDailyList(){
		try{
			Connection conn = getConnect();
			String sql = "select * from t_sys_etl_daily order by name";
			PreparedStatement pst = 
				conn.prepareStatement(sql);
			ResultSet rs = pst.executeQuery();
			List<EtlDailyVo> l = new ArrayList();
			while(rs.next()){
				EtlDailyVo evo = new EtlDailyVo();
				evo.class_name = rs.getString("class_name");
				evo.finish_time = rs.getDate("finish_time");
				evo.finish_time_str = DateTools.dtsWithHour(evo.finish_time);
				evo.item_name = rs.getString("name");
				evo.stat_time = rs.getDate("stat_time");
				evo.stat_time_str = DateTools.dtsWithHour(evo.stat_time);
				evo.status = rs.getInt("status");
				if(evo.status == 0){
					evo.status_str = "完成";
				}else if(evo.status == 1){
					evo.status_str = "进行中";
				}else{
					evo.status_str = "错误";
				}
				l.add(evo);
			}
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	
	
	
	
	public Connection getSourceConn(){
		Connection conn=null;
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
            String url = "jdbc:db2://172.16.95.131:50000/HIS"; 
            Properties props = new Properties();
            props.setProperty("user", "jxgl");
            props.setProperty("password", "lgxj");
            conn=DriverManager.getConnection(url,props);
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
        
	}
	public Connection getSourceChengBenConn(){
		Connection conn= null;
        try {
        	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn=DriverManager.getConnection("jdbc:sqlserver://172.16.95.169:1433;DatabaseName=HBOS",
            		"jxgl","jxgl");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
	}
	public Connection getTargetConn(){
		Connection conn= null;
        try {
        	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=PZSYYDB",
            		"performance","performance");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
	}
	
	
	
	
	
	
	
}
